// DlgEditStatus.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditStatus.h"
#include "DlgPickList.h"

// CDlgEditStatus dialog

IMPLEMENT_DYNAMIC(CDlgEditStatus, CDialog)

CDlgEditStatus::CDlgEditStatus(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditStatus::IDD, pParent)
	, m_nID(-1)
	, m_bAdd(FALSE)
	, m_szID(_T(""))
	, m_szCode(_T(""))
	, m_szDescription(_T(""))
	, m_szIconFile(_T(""))
	, m_szMessage(_T(""))
	, m_szExplanation(_T(""))
	, m_szNotify(_T(""))
	, m_bSendILL(FALSE)
	, m_bComplete(FALSE)
	, m_szInformation(_T(""))
	, m_szSubject(_T(""))
	, m_szMessageFile(_T(""))
	, m_szMessageFileExt(_T(""))
{
	m_nIndexID = -1;
}

CDlgEditStatus::~CDlgEditStatus()
{
}

void CDlgEditStatus::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Text(pDX, IDC_EDIT_CODE, m_szCode);
	DDX_Text(pDX, IDC_EDIT_DESCRIPTION, m_szDescription);
	DDX_Text(pDX, IDC_EDIT_MESSAGE, m_szMessage);
	DDX_Text(pDX, IDC_EDIT_ICON_FILE, m_szIconFile);
	DDV_MaxChars(pDX, m_szIconFile, 255);
	DDX_Text(pDX, IDC_EDIT_EXPLANATION, m_szExplanation);
	DDV_MaxChars(pDX, m_szDescription, 128);
	DDV_MaxChars(pDX, m_szExplanation, 128);
	DDX_Control(pDX, IDC_SPIN_RECORD, m_ctrlRecord);
	DDX_Text(pDX, IDC_EDIT_NOTIFY_EMAIL, m_szNotify);
	DDX_Check(pDX, IDC_CHECK_SEND_ILL, m_bSendILL);
	DDX_Check(pDX, IDC_CHECK_COMPLETE, m_bComplete);
	DDX_Text(pDX, IDC_EDIT_INFORMATION, m_szInformation);
	DDX_Text(pDX, IDC_EDIT_STATUS_SUBJECT, m_szSubject);
	DDV_MaxChars(pDX, m_szSubject, 128);
	DDX_Text(pDX, IDC_EDIT_MESSAGE_FILE, m_szMessageFile);
	DDX_Text(pDX, IDC_EDIT_MESSAGE_FILE_EXT, m_szMessageFileExt);
}


BEGIN_MESSAGE_MAP(CDlgEditStatus, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditStatus::OnBnClickedButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, &CDlgEditStatus::OnBnClickedButtonBrowse)
	ON_BN_CLICKED(IDC_BUTTON_APPLY, &CDlgEditStatus::OnBnClickedButtonApply)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_RECORD, &CDlgEditStatus::OnDeltaposSpinRecord)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE_MESSAGE_FILE, &CDlgEditStatus::OnBnClickedButtonBrowseMessageFile)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE_MESSAGE_FILE_EXT, &CDlgEditStatus::OnBnClickedButtonBrowseMessageFileExt)
END_MESSAGE_MAP()


// CDlgEditStatus message handlers

BOOL CDlgEditStatus::OnInitDialog()
{
	CDialog::OnInitDialog();

	// show warning
	AfxMessageBox( _T("Warning:\n\nAdding or Deleting Status IDs may cause detrimental effects.\n\nPlease see manual for information on Status IDs.") );

	STATUS_LOOKUP Status;
	CStringArray szaStatus;

	appConfig.m_SQL.GetDescriptions( CMySQLEx::status, &szaStatus, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );

	// check if adding
	if( m_bAdd )
	{
		GetDlgItem( IDC_BUTTON_DELETE )->EnableWindow( FALSE );
		return TRUE;
	}
	else
	{
		GetDlgItem( IDC_EDIT_ID )->EnableWindow( FALSE );
	}

	// hide external
	if( appConfig.m_External.szDatabase.IsEmpty() )
	{
		GetDlgItem( IDC_STATIC_MESSAGE_FILE_EXT )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_MESSAGE_FILE_EXT )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_BUTTON_BROWSE_MESSAGE_FILE_EXT )->ShowWindow( SW_HIDE );
	}

	// pick list
	if( m_nID < 0 )
	{
		// show pick list
		CDlgPickList PickListDlg;
		PickListDlg.m_pszaStrings = &szaStatus;
		if( PickListDlg.DoModal() == IDOK )
			m_nID = _tstol( PickListDlg.m_szString.Left( PickListDlg.m_szString.Find( _T(" ") )));
		else
		{
			EndDialog( IDCANCEL );
			return TRUE;
		}
	}

	// get status
	if( !appConfig.m_SQL.GetDescriptionStatus( &Status, m_nID ))
	{
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Status.szDescription;
	m_szExplanation = Status.szExplanation;
	m_szSubject = Status.szSubject;
	m_szMessage = Status.szMessage;
	m_szCode = Status.szCode;
	m_szIconFile = Status.szIconFile;
	m_szNotify = Status.szNotify;
	m_bSendILL = (BOOL)Status.nSendILL;
	m_bComplete = (BOOL)Status.nComplete;
	m_szInformation = Status.szInformation;
	m_szMessageFile = Status.szMessageFile;
	m_szMessageFileExt = Status.szMessageFileExt;

	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// disable ILL option
	if( appConfig.m_Options.bDisableILL || m_nID < STATUS_OFFSET_NA || m_nID == STATUS_ID_COMPLETE )
		GetDlgItem( IDC_CHECK_SEND_ILL )->ShowWindow( SW_HIDE );

	// disable complete
	if( m_nID == STATUS_ID_COMPLETE )
		GetDlgItem( IDC_CHECK_COMPLETE )->ShowWindow( SW_HIDE );

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditStatus::OnOK()
{
	// update record
	UpdateRecord();
	// close dialog
	CDialog::OnOK();
}

void CDlgEditStatus::OnBnClickedButtonApply()
{
	// update record
	UpdateRecord();
}

void CDlgEditStatus::OnBnClickedButtonDelete()
{
	// update data
	UpdateData();

	// get method id
	m_nID = _tstol( m_szID );

	// check for 0
	if( m_nID == 0 )
	{
		CString szMessage;
		AfxFormatString1( szMessage, IDS_ERROR_DELETE_ITEM_0, _T("Method") );
		AfxMessageBox( szMessage );
		return;
	}
	// check if sure
	if( AfxMessageBox( IDS_STATUS_DELETE, MB_YESNO ) == IDNO )
		return;

	// remove row
	appConfig.m_SQL.RemoveRow( CMySQLEx::status, m_nID );

	// adjust requests
	appConfig.m_SQL.UpdateFieldValue( CMySQLEx::requests, _T("status_id"), m_nID, 0 );

	// remove from array
	m_naID.RemoveAt( m_nIndexID );
	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// reset index
	m_nIndexID = 0;
	m_nID = m_naID.GetAt( m_nIndexID );

	// get priority
	STATUS_LOOKUP Status;
	if( !appConfig.m_SQL.GetDescriptionStatus( &Status, m_nID ))
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Status.szDescription;
	m_szExplanation = Status.szExplanation;
	m_szSubject = Status.szSubject;
	m_szMessage = Status.szMessage;
	m_szCode = Status.szCode;
	m_szIconFile = Status.szIconFile;
	m_szNotify = Status.szNotify;
	m_bSendILL = (BOOL)Status.nSendILL;
	m_bComplete = (BOOL)Status.nComplete;
	m_szInformation = Status.szInformation;
	m_szMessageFile = Status.szMessageFile;
	m_szMessageFileExt = Status.szMessageFileExt;

	// close
	CDialog::OnOK();
}

void CDlgEditStatus::OnBnClickedButtonBrowse()
{
	// open ico or bmp file	
	TCHAR szFilter[] = _T("Icon Files (*.ico)|*.ico|Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*||");
	CFileDialog fileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter );
	// show dialog
	if( fileDlg.DoModal() == IDOK )
	{
		m_szIconFile = fileDlg.GetPathName();
		// update data
		UpdateData( FALSE );
	}
}

void CDlgEditStatus::OnBnClickedButtonBrowseMessageFile()
{
	// open html file
	TCHAR szFilter[] = _T("HTML Files (*.html)|*.html|HTM Files (*.htm)|*.htm|All Files (*.*)|*.*||");
	CFileDialog fileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter );
	// show dialog
	if( fileDlg.DoModal() == IDOK )
	{
		m_szMessageFile = fileDlg.GetPathName();
		// update data
		UpdateData( FALSE );
	}
}

void CDlgEditStatus::OnBnClickedButtonBrowseMessageFileExt()
{
	// open html file
	TCHAR szFilter[] = _T("HTML Files (*.html)|*.html|HTM Files (*.htm)|*.htm|All Files (*.*)|*.*||");
	CFileDialog fileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter );
	// show dialog
	if( fileDlg.DoModal() == IDOK )
	{
		m_szMessageFileExt = fileDlg.GetPathName();
		// update data
		UpdateData( FALSE );
	}
}

void CDlgEditStatus::OnDeltaposSpinRecord(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

	// increment index
	m_nIndexID += pNMUpDown->iDelta;
	if( m_nIndexID > m_naID.GetUpperBound() )
	{
		Beep( 750, 300 );
		m_nIndexID = m_naID.GetUpperBound();
		//m_nIndexID = 0;
		return;
	}
	if( m_nIndexID < 0 )
	{
		Beep( 750, 300 );
		m_nIndexID = 0;
		//m_nIndexID = (INT)m_naID.GetUpperBound();
		return;
	}

	// get priority
	STATUS_LOOKUP Status;
	if( !appConfig.m_SQL.GetDescriptionStatus( &Status, m_naID.GetAt( m_nIndexID )))
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get id
	m_nID = Status.nID;

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = Status.szDescription;
	m_szExplanation = Status.szExplanation;
	m_szSubject = Status.szSubject;
	m_szMessage = Status.szMessage;
	m_szCode = Status.szCode;
	m_szIconFile = Status.szIconFile;
	m_szNotify = Status.szNotify;
	m_bSendILL = (BOOL)Status.nSendILL;
	m_bComplete = (BOOL)Status.nComplete;
	m_szInformation = Status.szInformation;
	m_szMessageFile = Status.szMessageFile;
	m_szMessageFileExt = Status.szMessageFileExt;

	// disable ILL option
	if( appConfig.m_Options.bDisableILL || m_nID < STATUS_OFFSET_NA || m_nID == STATUS_ID_COMPLETE )
		GetDlgItem( IDC_CHECK_SEND_ILL )->ShowWindow( SW_HIDE );
	else
		GetDlgItem( IDC_CHECK_SEND_ILL )->ShowWindow( SW_SHOW );

	//// disable complete
	//if( m_nID == STATUS_ID_COMPLETE )
	//{
	//	GetDlgItem( IDC_CHECK_COMPLETE )->ShowWindow( SW_HIDE );
	//	GetDlgItem( IDC_EDIT_MESSAGE_FILE )->EnableWindow( FALSE );
	//	GetDlgItem( IDC_BUTTON_BROWSE_MESSAGE_FILE )->EnableWindow( FALSE );
	//}
	//else
	//{
	//	GetDlgItem( IDC_EDIT_MESSAGE_FILE )->EnableWindow();
	//	GetDlgItem( IDC_BUTTON_BROWSE_MESSAGE_FILE )->EnableWindow();
	//}

	//update data
	UpdateData( FALSE );

	*pResult = 0;
}

BOOL CDlgEditStatus::UpdateRecord()
{
	// update data
	UpdateData();

	// check data
	if( m_szID.IsEmpty() || m_szDescription.IsEmpty() )
	{
		AfxMessageBox( _T("Data missing.\n\nPlease complete all data fields.") );
		return FALSE;
	}

	// get id
	m_nID = _tstol( m_szID );


	// check for overwrite
	if( m_bAdd )
	{
		if( appConfig.m_SQL.GetRowCount( CMySQLEx::status, m_nID ) > 0 )
		{
			if( AfxMessageBox( IDS_STATUS_CHANGE, MB_YESNO ) == IDNO )
			{
				// return
				return FALSE;
			}
		}
	}


	CStringArray szaStatus;
	STATUS_LOOKUP Status;

	// set variables
	Status.nID = m_nID;
	Status.szDescription = m_szDescription;
	Status.szExplanation = m_szExplanation;
	Status.szSubject = m_szSubject;
	Status.szMessage = m_szMessage;
	Status.szCode = m_szCode;
	Status.szIconFile = m_szIconFile;
	Status.szNotify = m_szNotify;
	Status.nSendILL = m_bSendILL;
	Status.nComplete = m_bComplete;
	Status.szInformation = m_szInformation;
	Status.szMessageFile = m_szMessageFile;
	Status.szMessageFileExt = m_szMessageFileExt;

	// save it
	appConfig.m_SQL.SetDescriptionStatus( &Status, m_nID, m_bAdd );

	// fetch collection
	appConfig.m_SQL.GetDescriptions( CMySQLEx::status, &szaStatus, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// update data
	UpdateData( FALSE );
	return TRUE;
}
